<?php
	class AioModel extends CI_Model {
		// 表名
		static $aioTable = "aio";
		static $aioTaskTable = "aio_task";

	    public function __construct() {
	        parent::__construct();
	    }
		
		// 获取一体机列表
		public function get_aio_list($params = ""){
			// 查询
			$where = "1 AND 1";
			if(isset($params['schoolId'])) $where .= " AND `school_id`='{$params['schoolId']}'";
			if(isset($params['campusId'])) $where .= " AND `campus_id`='{$params['campusId']}'";
			// 删除状态
			if(isset($params['isDelete'])) $where .= " AND `is_delete`='{$params['isDelete']}'";
			$this->db->where($where);
			// 分页
			if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
			// 是否只获取条数
			if(isset($params['getRowNumber'])) return $this->db->count_all_results(self::$aioTable);
			return $this->db->order_by('id ASC')->get(self::$aioTable)->result_array();
		}
		
		// 获取一体机信息
		public function get_aio_info($id){
			$where = "`id`='{$id}'";
			$aioInfo = $this->db->where($where)->get(self::$aioTable)->row_array();
			return $aioInfo;
		}

		// 获取一体机信息，通过facesluice_id和school_id
		public function get_aio_info_by_facesluice_id($schoolId, $facesluiceId) {
			$where = "`is_delete` = 0 AND `school_id`='{$schoolId}' AND `facesluice_id`='{$facesluiceId}'";
			$aioInfo = $this->db->where($where)->get(self::$aioTable)->row_array();
			return $aioInfo;
		}

		// 获取一体机信息，通过facesluice_id
		public function get_aio_info_by_facesluice_id_without_school_id($facesluiceId) {
			$where = "`is_delete` = 0 AND `facesluice_id`='{$facesluiceId}'";
			$aioInfo = $this->db->where($where)->get(self::$aioTable)->row_array();
			return $aioInfo;
		}

		// 获取一体机任务列表
		public function get_aio_task_list($params = ""){
			// 查询
			$where = "1 AND 1";
			if(isset($params['schoolId'])) $where .= " AND `school_id`='{$params['schoolId']}'";
			if(isset($params['campusId'])) $where .= " AND `campus_id`='{$params['campusId']}'";
			// 一体机id
			if(isset($params['aioId'])) $where .= " AND `aio_id`='{$params['aioId']}'";
			// 学生名称
			if(isset($params['realname'])) $where .= " AND `user_name` LIKE '%{$params['realname']}%'";
			// 任务状态
			if(isset($params['taskState'])) $where .= " AND `task_state`='{$params['taskState']}'";
			// 任务类型
			if(isset($params['taskType'])) $where .= " AND `task_type`='{$params['taskType']}'";
			// 删除状态
			if(isset($params['isDelete'])) $where .= " AND `is_delete`='{$params['isDelete']}'";
			$this->db->where($where);
			// 分页
			if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
			// 是否只获取条数
			if(isset($params['getRowNumber'])) return $this->db->count_all_results(self::$aioTaskTable);
			// 排序
			if(isset($params['orderBy'])) {
				$this->db->order_by($params['orderBy']);
			} else {
				$this->db->order_by('id DESC');
			}
			return $this->db->get(self::$aioTaskTable)->result_array();
		}
		
		// 获取一体机任务信息
		public function get_aio_task_info($id){
			$where = "`id`='{$id}'";
			$aioTaskInfo = $this->db->where($where)->get(self::$aioTaskTable)->row_array();
			return $aioTaskInfo;
		}

		//检查设备编码是否存在
		public function check_aio_facesluice_id_exists($schoolId, $campusId, $facesluiceId, $id = 0){
			$this->db->where("facesluice_id", $facesluiceId);
			$this->db->where("school_id", $schoolId);
			$this->db->where("campus_id", $campusId);
			$this->db->where('is_delete', 0);
			if($id){
				$this->db->where("id !=", $id);
			}
			return $this->db->get(self::$aioTable)->row_array();
		}
		
	}